package com.sinoglobal.ningxia.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.mico.ProgramInfoActivity;
import com.sinoglobal.ningxia.beans.MicChannelVo;
import com.sinoglobal.ningxia.beans.MicProgramBean;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.ConnectionUtil;
import com.sinoglobal.ningxia.utils.EquipmentUtil;
import com.sinoglobal.ningxia.utils.FlyUtil;

import net.tsz.afinal.FinalBitmap;

import java.util.List;

/**
 * @author qiwx
 * @createdate 2014-6-20 下午3:22:06
 * @Description: 微视听适配器
 */
public class MicChannelAdapter extends BaseAdapter {

	List<MicChannelVo> channelList;
	Activity activity;
	LayoutInflater mInflater;
	List<MicProgramBean> programList;
	FinalBitmap fb;
	Bitmap defaultPic;
	ImageView iv;

	public MicChannelAdapter(Activity activity, List<MicChannelVo> list) {
		this.channelList = list;
		this.activity = activity;
		mInflater = LayoutInflater.from(activity);
		defaultPic = BitmapFactory.decodeResource(activity.getResources(), R.drawable.default43);
		fb = FinalBitmap.create(activity);
		iv = new ImageView(activity);
	}

	@Override
	public int getCount() {
		return channelList.size();
	}

	@Override
	public Object getItem(int position) {
		return channelList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int channelPosition, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			viewholder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.mic_channel_listview_item, null);
			viewholder.setChannelName((TextView) convertView.findViewById(R.id.mic_channel_name));
			viewholder.setProgram((GridView) convertView.findViewById(R.id.mic_program_gridview));
			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		// 频道
		MicChannelVo channelBean = channelList.get(channelPosition);
		// 频道名字
		viewholder.getChannelName().setText(channelBean.getProgram());
		// 栏目列表
		final List<MicProgramBean> programList = channelBean.getLanmu();
		ProgramAdapter programAdapter = new ProgramAdapter(programList);
		viewholder.getProgram().setAdapter(programAdapter);
		viewholder.getProgram().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int programPosition, long id) {
				//Auto-generated method stub
				// Toast.makeText(activity,channelPosition+"栏目"+programPosition+"节目",
				// Toast.LENGTH_SHORT).show();
				MicProgramBean bean = programList.get(programPosition);
				Intent go = new Intent();
				go.putExtra(MicProgramBean.class.getName(), bean);
				go.setClass(activity, ProgramInfoActivity.class);
				FlyUtil.intentForwardNetWork(activity, go);

			}

		});
		return convertView;
	}

	class ViewHolder {
		TextView channelName;
		GridView program;

		public TextView getChannelName() {
			return channelName;
		}

		public void setChannelName(TextView channelName) {
			this.channelName = channelName;
		}

		public GridView getProgram() {
			return program;
		}

		public void setProgram(GridView program) {
			this.program = program;
		}

	}

	class ProgramAdapter extends BaseAdapter

	{
		// 动态设置Viewitem的高度，图片宽高比例为4:3

		int ImgWidth;
		int ImgHeight;
		List<MicProgramBean> list;

		public ProgramAdapter(List<MicProgramBean> list) {
			this.list = list;
			ImgWidth = (FlyApplication.widthPixels - EquipmentUtil.dip2px(activity, 52)) / 3;
			ImgHeight = 3 * (ImgWidth) / 4;

		}

		@Override
		public int getCount() {

			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder1 viewholder;

			if (convertView == null) {
				viewholder = new ViewHolder1();
				convertView = mInflater.inflate(R.layout.mic_program_gridview_item, null);
				viewholder.setProgramThumb((ImageView) convertView.findViewById(R.id.programThumb));
				viewholder.setProgramName((TextView) convertView.findViewById(R.id.programName));
				viewholder.setLabelHot((TextView) convertView.findViewById(R.id.label_hot));
				viewholder.setLabelStatus((TextView) convertView.findViewById(R.id.label_status));
				viewholder.setRelativeLayout((RelativeLayout) convertView.findViewById(R.id.rela));
				convertView.setTag(viewholder);
			} else {
				viewholder = (ViewHolder1) convertView.getTag();
			}
			MicProgramBean bean = list.get(position);
			if (null == bean)
				return null;

			String url = ConnectionUtil.localUrl + bean.getLanmuimage();
			viewholder.getRelativeLayout().setLayoutParams(new LinearLayout.LayoutParams(ImgWidth, ImgHeight));
			fb.display(viewholder.getProgramThumb(), url, ImgWidth, ImgHeight);
			int isGoing = 0;// 1代表正在播出
			int hot = 0;// 1代表热门
			try {
				isGoing = Integer.valueOf(bean.getBochu());
				hot = Integer.valueOf(bean.getHot());
			} catch (ClassCastException e) {
				e.printStackTrace();
			}
			if (hot == 1) {
				viewholder.getLabelHot().setText(R.string.isHot);
				viewholder.getLabelHot().setVisibility(View.VISIBLE);
				// viewholder.getLabelStatus().setTextColor(activity.getResources().getColor(R.color.red));
				if (isGoing == 1) {
					viewholder.getLabelStatus().setText(R.string.isStatus);
					// viewholder.getLabelStatus().setTextColor(activity.getResources().getColor(R.color.green));
					viewholder.getLabelStatus().setVisibility(View.VISIBLE);

				} else {
					viewholder.getLabelStatus().setVisibility(View.INVISIBLE);

				}

			} else {
				if (isGoing == 1) {
					viewholder.getLabelHot().setText(R.string.isStatus);
					viewholder.getLabelHot().setVisibility(View.VISIBLE);
					viewholder.getLabelHot().setBackgroundColor(activity.getResources().getColor(R.color.green));
					viewholder.getLabelStatus().setTextColor(activity.getResources().getColor(R.color.white));
					viewholder.getLabelStatus().setVisibility(View.INVISIBLE);
				} else {
					viewholder.getLabelHot().setVisibility(View.INVISIBLE);
					viewholder.getLabelStatus().setVisibility(View.INVISIBLE);
				}

			}

			/*
			 * if (isGoing == 1) {
			 * viewholder.getLabelStatus().setText(R.string.isStatus);
			 * viewholder
			 * .getLabelStatus().setTextColor(activity.getResources().getColor
			 * (R.color.red));
			 * viewholder.getLabelStatus().setVisibility(View.VISIBLE); if (hot
			 * == 1) { viewholder.getLabelHot().setText(R.string.isHot);
			 * viewholder.getLabelHot().setVisibility(View.VISIBLE);
			 * viewholder.getLabelStatus
			 * ().setTextColor(activity.getResources().getColor(R.color.green));
			 * } else {
			 * viewholder.getLabelStatus().setVisibility(View.INVISIBLE);
			 * viewholder
			 * .getLabelStatus().setTextColor(activity.getResources().getColor
			 * (android.R.color.transparent)); }
			 * 
			 * } else { //
			 * viewholder.getLabelStatus().setVisibility(View.INVISIBLE); if
			 * (hot == 1) {
			 * viewholder.getLabelStatus().setText(R.string.isStatus);
			 * viewholder
			 * .getLabelStatus().setTextColor(activity.getResources().getColor
			 * (R.color.green));
			 * viewholder.getLabelStatus().setVisibility(View.VISIBLE); } else {
			 * viewholder.getLabelStatus().setVisibility(View.INVISIBLE);
			 * viewholder.getLabelHot().setVisibility(View.INVISIBLE);
			 * viewholder
			 * .getLabelStatus().setTextColor(activity.getResources().getColor
			 * (android.R.color.transparent));
			 * viewholder.getLabelStatus().setTextColor
			 * (activity.getResources().getColor(android.R.color.transparent));
			 * } }
			 */

			viewholder.getProgramName().setText(bean.getLanmuname());
			return convertView;
		}

		class ViewHolder1 {
			ImageView programThumb;
			TextView programName;
			TextView labelHot;
			TextView labelStatus;
			RelativeLayout relativeLayout;

			public RelativeLayout getRelativeLayout() {
				return relativeLayout;
			}

			public void setRelativeLayout(RelativeLayout relativeLayout) {
				this.relativeLayout = relativeLayout;
			}

			public ImageView getProgramThumb() {
				return programThumb;
			}

			public void setProgramThumb(ImageView programThumb) {
				this.programThumb = programThumb;
			}

			public TextView getProgramName() {
				return programName;
			}

			public void setProgramName(TextView programName) {
				this.programName = programName;
			}

			public TextView getLabelHot() {
				return labelHot;
			}

			public void setLabelHot(TextView labelHot) {
				this.labelHot = labelHot;
			}

			public TextView getLabelStatus() {
				return labelStatus;
			}

			public void setLabelStatus(TextView labelStatus) {
				this.labelStatus = labelStatus;
			}
		}
	}
}
